# *-* coding: utf-8 *-*
"""
@author: Hekang Xu (E-mail:1092181938@qq.com)
"""

import os

import cv2
from common.exception.FolderNotExsitError import FolderNotExsitError
import common.util.PrintUtil as printUtil
from predata.Input import input_image


def prepare_image(input_path, out_put_path):
    try:
        print("输入文件路径：", input_path)
        print("正在预处理文件...")
        multi_files = input_image(input_path)
        for i, file in enumerate(multi_files.all_files):
            try:
                out_image_path = out_put_path + '/' + os.path.basename(file)
                resize_image(file, out_image_path)
                printUtil.out_rate(percent_bool=True, total_num=multi_files.total_num, now_num=i, per_show=10)
            except FileNotFoundError as error:
                print("处理失败的文件：", str(error))
        # all_output_path = output_images(multi_files.all_files, out_put_path)
        print("输出文件完成... ")
        print("处理文件数量：", multi_files.total_num)
        print("文件输出路径：", out_put_path)
    except FolderNotExsitError as error:
        print(str(error), "请检查文件路径，路径不存在：" + input_path)
        # 返回错误，处理失败
        return False


def resize_image(input_image_path, out_image_path):
    """
    预处理文件大小

    cv2.resize(src,dsize[,dst[,fx[,fy[,interpolation]]]]) -> dst
    src:输入图像，dsize:输出图像的大小，如果为0，dsize=fx*src.cols , fy*src.rows
    fx:水平放缩尺寸，如果为0，size.width/src.cols，fy:垂直放缩尺寸，如果为0，
    interpolation:插值方法，有以下选项可以选择
    INTER_NEAREST:近邻插值
    INTER_LINERA:双线性插值，默认
    INTER_CUBIC:三次内插（4*4的像素邻居）
    INTER_LANCZOS4
    INTER_AREA：
    """
    if os.path.exists(input_image_path):
        img = cv2.imread(input_image_path)
        res = cv2.resize(img, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_CUBIC)
        cv2.imwrite(out_image_path, res)
    else:
        raise FileNotFoundError(input_image_path)


if __name__ == "__main__":
    prepare_image("/Users/xuhekang/Desktop/图片/image_judge/origin", "/Users/xuhekang/Desktop/图片/image_judge/train")
